package com.lh.project.controller;

import com.lh.project.common.BaseResponse;
import com.lh.project.common.RedisUtil;
import com.lh.project.common.ResultUtils;
import com.lh.project.common.UserHolder;
import com.lh.project.model.entity.Comment;
import com.lh.project.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     * 查询评论
     * @param id
     * @param current
     * @return
     */
    @GetMapping("commentList")
    public BaseResponse<List<Comment>> getCommentListByFoodId(@RequestParam Long id,@RequestParam(value = "current",defaultValue = "1") Integer current){
//    public BaseResponse<List<Comment>> getCommentListByFoodId(@RequestParam(value = "current",defaultValue = "1") int current){
//        Long id = UserHolder.getUser().getId();
        System.out.println("FoodId="+id);
        if(id==null){
            return ResultUtils.error(null);
        }
        return ResultUtils.success(commentService.getCommentList(id,current));
    }

    /**
     * 创建一级评论
     * @return
     */
    @PostMapping("post")
    public BaseResponse<String> createOneComment(@RequestBody Comment comment){
        //一级评论时简单的插入即可！
        comment.setUserId(UserHolder.getUser().getId());
        commentService.save(comment);
        return ResultUtils.success("评论成功!");
    }

    /**
     *
     * @param commentId 表示被点赞的评论id
     * @return
     */
    @PostMapping("/like")
    public BaseResponse<String> likeComment(@RequestBody Long commentId){
        return commentService.likeComment(UserHolder.getUser().getId(),commentId);
    }
}
